﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Annotations;

namespace SharedGenomics.Workbench.Tests.Annotations
{
    [TestClass]
    public class AnnotationInfoTests
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorShouldThrowIfDescriptorNull()
        {
            var info = new AnnotationInfo(null);
        }

        [TestMethod]
        public void ConstructorShouldSetDescriptor()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.GoogleSearch);
            var info = new AnnotationInfo(descriptor);
            Assert.AreEqual(descriptor, info.Descriptor);
            Assert.AreEqual(AnnotationRequestStatus.Pending, info.Status);
        }

        [TestMethod]
        public void FinishShouldUpdateState()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.GoogleSearch);
            var info = new AnnotationInfo(descriptor);

            IAnnotation gsa = new GoogleSearchAnnotation("rs001");
            info.Finish(gsa);

            Assert.AreEqual(gsa, info.Value);
            Assert.AreEqual(AnnotationRequestStatus.Finished, info.Status);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void FinishShouldThrowIfFrozen()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.GoogleSearch);
            var info = new AnnotationInfo(descriptor);

            IAnnotation gsa = new GoogleSearchAnnotation("rs001");
            info.Fail();
            info.Finish(gsa);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FinishShouldThrowIfAnnotationNull()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.GoogleSearch);
            var info = new AnnotationInfo(descriptor);

            info.Finish(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void FinishShouldThrowIfAnnotationInvalid()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.NcbiSummary);
            var info = new AnnotationInfo(descriptor);

            info.Finish(new GoogleSearchAnnotation("rs001"));
        }

        [TestMethod]
        public void FailShouldUpdateState()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.GoogleSearch);
            var info = new AnnotationInfo(descriptor);

            info.Fail();
            Assert.AreEqual(AnnotationRequestStatus.Failed, info.Status);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void FailShouldThrowIfFrozen()
        {
            AnnotationDescriptor descriptor = AnnotationDescriptor.GetDescriptorForKey(AnnotationKey.GoogleSearch);
            var info = new AnnotationInfo(descriptor);

            info.Fail();
            info.Fail();
        }
    }
}
